package com.summer.safe.tag;


import com.summer.safe.utils.CollectionKit;
import com.summer.safe.utils.TagUtil;
import org.beetl.core.tag.GeneralVarTagBinding;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *  select标签
 */
public class SelectTag extends GeneralVarTagBinding {
    //开启校验
    private final static String layVerify = "layVerify";
    //是否开启搜索
    private final static String laySearch = "laySearch";
    //去掉样式渲染(原生)
    private final static String layIgnore = "layIgnore";
    //定义事件过滤器
    private final static String layFilter = "layFilter";
    //开启占位符
    private final static String placeholder = "placeholder";
    //数据集
    private final static String items = "items";
    //实际值
    private final static String itemValue = "itemValue";
    //显示值
    private final static String itemLabel = "itemLabel";
    //值
    private final static String value = "value";
    //id、名称
    private final static String path = "path";
    //是否禁用
    private final static String disabled = "disabled";

    @Override
    public void render() {
        String tagName = (String) this.args[0];
        Map attrs = (Map) args[1];

        String select = "<select name='" + attrs.get(path) + "' ";

        List<String> options = new ArrayList<>();
        try {
            Iterator entries = attrs.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry entry = (Map.Entry) entries.next();
                String key = (String) entry.getKey();
                if (layVerify.equals(key)) {
                    select += "lay-verify='required' ";
                }
                if (laySearch.equals(key)) {
                    select += "lay-search ";
                }
                if (layIgnore.equals(key)) {
                    select += "lay-ignore ";
                }
                if (layFilter.equals(key)) {
                    select += "lay-filter='" + attrs.get(layFilter) + "' ";
                }
                if (disabled.equals(key)) {
                    select += "disabled='" + attrs.get(disabled) + "' ";
                }
                if (value.equals(key)) {
                    select += "data-value=" + attrs.get(value) + " ";
                }
                if (placeholder.equals(key)) {
                    options.add("<option value=''>" + attrs.get(placeholder) + "</option>");
                }
            }
            select += ">";
            select += CollectionKit.join(getOptions(options, attrs), "");
            select += "</select>";
            this.ctx.byteWriter.writeString(select);
        } catch (IOException e) {

        }
    }

    private List<String> getOptions(List<String> options, Map attrs) {
        Object itemsMap = attrs.get(items);
        //自定义的select格式的数据
        if (null != attrs.get(itemValue) && null != attrs.get(itemLabel)) {
            ((Map<Object, Object>) TagUtil.formatData(itemsMap, attrs.get(itemValue).toString(), attrs.get(itemLabel).toString())).forEach((k, v) -> {
                if (null != attrs.get(value) && k.equals(attrs.get(value))) {
                    options.add("<option value='" + k + "' selected>" + v + "</option>");
                } else {
                    options.add("<option value='" + k + "'>" + v + "</option>");
                }
            });
        } else {
            ((Map<Object, Object>) TagUtil.formatData(itemsMap)).forEach((k, v) -> {
                if (null != attrs.get(value) && k.equals(attrs.get(value))) {
                    options.add("<option value='" + k + "' selected>" + v + "</option>");
                } else {
                    options.add("<option value='" + k + "'>" + v + "</option>");
                }
            });
        }
        return options;
    }
}
